package fullsail;

import java.awt.Color;

import robocode.*;
import robocode.util.Utils;
import static robocode.util.Utils.*;

public class RobotSec extends TeamRobot
{
	String myTarget;
	double gunHeading;
	boolean bForward;
	int trackCount = 0;
	double turnAngle;
	int reverseCount = 0;
	double enemyEnergy;
	double prevEnemyEnergy;
	
    public void run()
    {
		setBodyColor(new Color(150,0,255));
		setGunColor(new Color(150,0,255));
		setRadarColor(new Color(150,0,255));
		setScanColor(Color.GREEN);
		setBulletColor(new Color(150,0,255));
    	
    	gunHeading = 0;
    	bForward = true;
    	//myTarget = null;
    	turnAngle = 30;
    	
    	setAdjustGunForRobotTurn(true);
    	setAdjustRadarForGunTurn(true);
    	setAdjustRadarForRobotTurn(true);
    	
        while (true)
        {
        	if(bForward)
        		setAhead(40000);
        	
        	waitFor(new TurnCompleteCondition(this));  
        	
        	// always be scanning, unless we get a target
        	turnRadarRightRadians(Double.POSITIVE_INFINITY);
        	
            //trackCount++;
            reverseCount++;
            
            // cite inspred by the Tracker bot example, numbers tweaked to my preference
//            if(trackCount > 1)
//            	gunHeading = -180;
//            if(trackCount > 4)
//            	gunHeading = 180;
//            if(trackCount > 7)
//            	myTarget = null;
            
                       
            closeToWall();
        }
    }
    public void onScannedRobot(ScannedRobotEvent e)
    {
    	enemyEnergy = e.getEnergy();
//    	if(myTarget != null && !e.getName().equals(myTarget))
//    		return;
    	
//    	if(myTarget == null)
//    	{
//    		myTarget = e.getName();
//    	}
    	//trackCount = 0;
    	//if(!isTeammate(e.getName()))
    	//{
    	// need to cite this algorithm for lock on targeting and leading shot
    		double absolute = getHeadingRadians()+ e.getBearingRadians();
    		setTurnRadarRightRadians(Utils.normalRelativeAngle(absolute - getRadarHeadingRadians())*1.5);
    		
    		gunHeading = Utils.normalRelativeAngle(absolute - getGunHeadingRadians());
    		
    		setTurnGunRightRadians(gunHeading + (e.getVelocity() * Math.sin(e.getHeadingRadians() - absolute) / 13.0));
    		
    		if(e.getDistance() < 50.0)
    			//time to ram
    		{
    			setTurnRightRadians(e.getBearing());
    			setFire(Math.min(3.0,getEnergy()));
    		}
    		else
    		{
    			setFire(Math.min(2.0,getEnergy()));
    			// movement to be perp to enemy
    			if(bForward)
    				setTurnRightRadians(90.0 + e.getBearing());
    			else
    				setTurnRightRadians(90.0 - e.getBearing());
    		}
    		//setFire(100-(e.getDistance()/getBattleFieldWidth()));
    		//setFire(3.0);
    		
    		// cite this stop go influenced type of movement, but im only reversing direction
    		if(enemyEnergy < prevEnemyEnergy)
    		{
    			// he may have fired!
    			reverseDirection();
    			reverseCount = 0;
    		}
    		if(reverseCount > 10)
    		{
    			reverseDirection();
    			reverseCount = 0;
    		}
    		prevEnemyEnergy = enemyEnergy;
    }
    public void onHitWall(HitWallEvent e)
    {
    	reverseDirection();
    	reverseCount = 0;
    }
    public void onHitRobot(HitRobotEvent e)
    {
    	reverseDirection();
    	reverseCount = 0;
    }
    public void reverseDirection()
    {
    	if(bForward)
    	{
    		setBack(40000);
    		bForward = false;
    	}
    	else
    	{
    		setAhead(40000);
    		bForward = true;
    	}
    }
    // this whole function doesn't really work! grr.....
    public void closeToWall()
    {
    	// here we will attempt to turn around if we get close to the wall
    	if(getX() < 40.0 ||  getX() > getBattleFieldWidth()-40.0)
    	{
    		// we have gotten too close on the X axis
    		reverseDirection();
    		reverseCount = 0;
    	}
    	if(getY() < 40.0 ||  getY() > getBattleFieldHeight()-40.0)
    	{
    		// we have gotten too close on the Y axis
    		reverseDirection();
    		reverseCount = 0;
    	}
    }
}

